/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.TextBox;
import org.gwtportlets.portlet.client.ui.CssButton;
import org.gwtportlets.portlet.client.ui.Dialog;
import org.gwtportlets.portlet.client.ui.LayoutPanel;
import org.gwtportlets.portlet.client.ui.Portlet;
import org.gwtportlets.portlet.client.util.FormBuilder;
import za.org.foobar.famon.frontend.client.BaseCallback;
import za.org.foobar.famon.frontend.client.FamonService;
import za.org.foobar.famon.frontend.client.dto.ApplicationDto;
import za.org.foobar.famon.frontend.client.framework.ModelObjectSuggestBox;

public class ApplicationEditDialog extends Dialog {
    private final ApplicationDto application;
    private TextBox name;
    private ModelObjectSuggestBox ownerSuggestBox;
    private CheckBox privateCheckBox;
    private Portlet portletToRefresh;

    public ApplicationEditDialog(final ApplicationDto application) {
        this(application, null);
    }

    public ApplicationEditDialog(final ApplicationDto application, Portlet portletToRefresh) {
        this.application = application;
        this.portletToRefresh = portletToRefresh;

        LayoutPanel layout = new LayoutPanel();
        Button btnSave = new CssButton("Save");
        setWidget(layout);

        if (application == null) {
            setText("New Application");
        } else {
            setText("Edit Application - " + application.name);
        }
        addButton(btnSave);
        addCloseButton();
        setSize("250px", "150px");
        center();

        name = new TextBox();
        ownerSuggestBox = new ModelObjectSuggestBox(new TextBox(), "UserData", "email", null);
        privateCheckBox = new CheckBox();

        // Set selected values
//        if (application != null) {
//            name.setText(application.name);
//            if (application.owner != null) {
//                ownerSuggestBox.setText(application.owner.email);
//                ownerSuggestBox.setSelectedId(application.ownerUserDataId);
//            }
//            privateCheckBox.setValue(application.isPrivate);
//        }

        FormBuilder form = new FormBuilder();
        form.label("Name").field(name).endRow()
            .label("Owner").field(ownerSuggestBox).endRow()
            .label("Private").field(privateCheckBox).endRow();
        layout.add(form.getFormInPanel());


//        ownerSuggestBox.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>() {
//            public void onSelection(SelectionEvent<SuggestOracle.Suggestion> suggestionSelectionEvent) {
//                Window.alert("Selected: " + ownerSuggestBox.getSelectedId());
//            }
//        });

        btnSave.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                FamonService.App.get().updateApplication(getUpdatedDto(), new BaseCallback() {
                    @Override
                    public void onSuccess(Object result) {
                        hide();
                        if (ApplicationEditDialog.this.portletToRefresh != null) {
                            ApplicationEditDialog.this.portletToRefresh.refresh();
                        }
                    }
                });
            }
        });

    }

    public Portlet getPortletToRefresh() {
        return portletToRefresh;
    }

    public void setPortletToRefresh(Portlet portletToRefresh) {
        this.portletToRefresh = portletToRefresh;
    }

    private ApplicationDto getUpdatedDto() {
        ApplicationDto app = new ApplicationDto();
        if (application != null) {
            app.id = application.id;
        }
//        app.prettyId = application.prettyId;
        app.name = name.getText();
//        app.ownerUserDataId = ownerSuggestBox.getSelectedId();
        app.isPrivate = privateCheckBox.getValue();
        return app;
    }
}
